<template>
  <div class="qs-chart"></div>
</template>

<script>
import echarts from 'echarts'
import { debounce } from '@/util'
// var dataAxis = ['点', '击', '柱', '子', '或', '者', '两', '指', '在', '触', '屏', '上', '滑', '动', '能', '够', '自', '动', '缩', '放']
// var data = [220, 182, 191, 234, 290, 330, 310, 123, 442, 321, 90, 149, 210, 122, 133, 334, 198, 123, 125, 220]
function buildOption(dataAxis, data, text, subtext = undefined) {
  const dataMax = Math.max(...data) || 0
  const yMax = Math.ceil(dataMax / 10) * 10 || 0

  return {
    title: {
      text: text,
      subtext: subtext,
      left: 'center'
    },
    xAxis: {
      data: dataAxis,
      axisLabel: {
        inside: true,
        textStyle: {
          color: '#fff'
        }
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      },
      z: 10
    },
    yAxis: {
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        textStyle: {
          color: '#999'
        }
      }
    },
    dataZoom: [
      {
        type: 'inside'
      }
    ],
    series: [
      { // For shadow
        type: 'bar',
        itemStyle: {
          normal: { color: 'rgba(0,0,0,0.05)' }
        },
        barGap: '-100%',
        barCategoryGap: '40%',
        data: data.map(() => yMax),
        animation: false
      },
      {
        type: 'bar',
        itemStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                { offset: 0, color: '#83bff6' },
                { offset: 0.5, color: '#188df0' },
                { offset: 1, color: '#188df0' }
              ]
            )
          },
          emphasis: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                { offset: 0, color: '#2378f7' },
                { offset: 0.7, color: '#2378f7' },
                { offset: 1, color: '#83bff6' }
              ]
            )
          }
        },
        data: data
      }
    ]
  }
}

export default {
  name: 'BarChartByType',
  props: {
    source: {
      type: Array,
      required: true
    },
    title: {
      type: String,
      default: '各类型题目统计'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    source() {
      this.destroy()
      this.create()
    }
  },
  mounted() {
    this.create()
  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    create() {
      this.initChart()
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    },
    destroy() {
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.__resizeHandler)
      this.chart.dispose()
      this.chart = null
    },
    initChart() {
      const chart = this.chart = echarts.init(this.$el, 'macarons')

      const source = this.source
      const title = this.title
      const dataAxis = source.map(item => item.name)
      const data = source.map(item => item.count)
      const zoomSize = 6

      chart.setOption(buildOption(dataAxis, data, title))
      chart.on('click', function(params) {
        chart.dispatchAction({
          type: 'dataZoom',
          startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
          endValue: dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
        })
      })
    }
  }
}
</script>
